package com.project.finance.product.controller;

import com.project.finance.common.po.TbProduct;
import com.project.finance.common.po.TbProductDetail;
import com.project.finance.common.resultconfig.Result;
import com.project.finance.common.resultconfig.ResultGenerator;
import com.project.finance.common.vo.ResultMsg;
import com.project.finance.product.service.ProductService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
* 理财产品管理
*
*/
@RestController
@RequestMapping("/api")
public class ProductController {
    private static final Logger logger = LogManager.getLogger(ProductController.class);
    @Autowired
    private ProductService productService;


    /**
     *
     * 根据页码和条数返回 结果
     * @return
     */
    @RequestMapping(value = "product",method = RequestMethod.GET)
    public ResultMsg listProductByPageCondition(@RequestParam("pageSize") int pageSize,
                                             @RequestParam("pageNum") int pageNum){
        ResultMsg resultMsg = productService.listTbProductByPage(pageSize, pageNum);
        resultMsg.setCode(0);
        return resultMsg;
    }

//
//    /*
//    * 查询所有理财产品信息
//    *
//    */
//    @RequestMapping(value = "products",method = RequestMethod.GET)
//    public Result listProduct(){
//        List<TbProduct> tbProducts = productService.listTbProduct();
//        return ResultGenerator.apiSuccess(tbProducts);
//    }

    /*
    * 增加理财产品信息
    */
    @RequestMapping(value="product",method = RequestMethod.POST)
    public Result insert(@RequestBody TbProduct tbProduct){
        int i = productService.insert(tbProduct);
        if (i>0){
            return ResultGenerator.apiSuccess("ok");
        }else {
            return ResultGenerator.apiError("error");
        }
    }

    /*
    * 修改理财产品
    */
    @RequestMapping(value = "product",method = RequestMethod.PUT)
    public Result update(@RequestBody TbProduct tbProduct){
        int i= productService.update(tbProduct);
        if (i>0){
            return ResultGenerator.apiSuccess("ok");
        }else {
            return ResultGenerator.apiError("error");
        }
    }

    /*
    * 删除理财产品
    */
    @RequestMapping(value="product",method = RequestMethod.DELETE)
    public Result delete(@RequestParam List<Integer> productId){
        int i = productService.delete(productId);
        if (i>0){
            return ResultGenerator.apiSuccess("ok");
        }else {
            return ResultGenerator.apiError("error");
        }

    }
    //根据理财产品名称来查询一个理财产品信息
     @RequestMapping(value = "products",method = RequestMethod.GET)
    public Result getProoduct(@RequestParam("name")String productdname){
        logger.info("GET Name : " + productdname);
        List<TbProduct> tbProducts = productService.getProoduct(productdname);
        Result listResult = ResultGenerator.apiSuccess(tbProducts);
        return listResult;
    }

}
